<?php
namespace app\common\service;

use app\common\model\Post as PostModel;
use app\common\model\PostClass as PostClassModel;
use think\Loader;

class Post{

    public function paginate($pageSize = 20){

        $postModel = new PostModel();

        $posts = $postModel -> order('id desc')->paginate($pageSize);

        return $posts;
    }

    /**
     * 根据id获取
     * @param $ids
     */
    public function all($ids){

        $postModel = new PostModel();
        $post = $postModel ->all($ids);
        return $post;
    }

    /**
     * @param $ids
     * @param int $pageSize
     * @return mixed
     */
    public function paginateByPc($pc_ids,$pageSize = 20){
        $postModel = new PostModel();
        $all = $postModel->whereIn('pc_id',$pc_ids)->order('id desc')->paginate($pageSize);
        return $all;
    }

    /**
     * 不用all获取单篇 用find 语义明确
     * @param $id
     * @return null|static
     */
    public function find($id){

        return PostModel::get($id);
    }

    public function insert($data){

        $postModel = new PostModel();

        $validate = Loader::validate("PostValidate");
        $checkResult = $validate ->scene("add") -> check($data);
        if(!$checkResult){
            return ['state'=>0,'message'=>$validate->getError()];
        }

        $result = $postModel->allowField(['title','pc_id','desc','cover'])->save($data);
        $postData = ['content'=>isset($data['content'])?$data['content']:''];
        $postModel->postData()->save($postData);

        if(false === $result){

            return ['state'=>0,'message'=>$postModel->getError()];
        }

        return ['state'=>1,'message'=>'','insertId'=>$postModel->id];

    }


    public function update($data){
        $postModel = new PostModel();

        if(intval($data['id']) <1){
            return ['state'=>0,'message'=>"参数错误"];
        }

        $validate = Loader::validate("PostValidate");
        $checkResult = $validate ->scene('edit') -> check($data);
        if(!$checkResult){
            return ['state'=>0,'message'=>$validate->getError()];
        }

        $result = $postModel->allowField(['id','title','pc_id','desc','cover'])->save($data,['id'=>intval($data['id'])]);
        $postModel = PostModel::get($data['id']);
        $postModel->postData-> content = isset($data['content'])?$data['content']:'';
        $postModel->postData->save();

        if(false === $result){
            return ['state'=>0,'message'=>$postModel->getError()];
        }

        return ['state'=>1,'message'=>''];
    }

    public function addPv($id){
        $postModel = PostModel::get($id);
        $postModel -> pv = intval($postModel->pv)+1;
        $postModel->save();
    }

    public function delete($id){

        $post = PostModel::get($id);
        $result = $post->together("postData")->delete();

        return $result;
    }

    /**
     *
     */
    public function getAllPostClass(){

        $postClassModel = new PostClassModel();

        return $postClassModel -> all();
    }
}
